package sampleapp.dao;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import sampleapp.model.User;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

@Singleton
public class UserDao {

	@Inject
	private Provider<EntityManager> emProvider;

	public User getUserByEmail(String email) {
		try {
			return (User) emProvider.get()
					.createQuery("select u from User u where email=:email")
					.setParameter("email", email).getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	public void save(User user) {
		emProvider.get().persist(user);
	}
}
